import { defineStore } from "pinia";

export const useUserStore = defineStore("user", {
  state: () => ({
    username: "",
    token: "",
  }),
  actions: {
    setUsername(username: string) {
      this.username = username;
      localStorage.setItem("username", username);
    },
    setToken(token: string) {
      this.token = token;
      localStorage.setItem("token", token);
    },
    loadState() {
      const savedUsername = localStorage.getItem("username");
      if (savedUsername) {
        this.username = savedUsername;
      }
      const savedToken = localStorage.getItem("token");
      if (savedToken) {
        this.token = savedToken;
      }
    },
  },
});
